package Offer001;

/**
 * 整数的除法
 *
 * @author 23737
 * @time 2021.12.6
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public int divide(int a, int b) {
        //使用位运算来进行解决
        if (a == Integer.MIN_VALUE && b == -1)
            return Integer.MAX_VALUE;

        //关键的步骤
        int sign = (a > 0) ^ (b > 0) ? -1 : 1;

        if (a > 0) a = -a;
        if (b > 0) b = -b;

        int res = 0;
        while (a <= b) {
            a -= b;
            res++;
        }

        return sign == 1 ? res : -res;
    }
}
